<?php
class CustomerAction extends SecurityAction {
	function _initialize() {
		$this->assign ( "menuFirst", session ( 'menuFirst' ) );
		$this->assign ( "menuSecond", session ( 'menuSecond' ) );
	}
	function clist() {
		if ($_GET ['opt'] == 'add') { // 新增跳转
			$info ['hash'] = $this->getHashCode ();
			$this->assign ( "info", $info );
			$this->display ( 'Customer/detail' );
			return;
		}
		
		if ($_GET ["opt"] == 'edit') { // 修改跳转
			$ob = M ( "csl_customer" );
			$info = $ob->where ( array (
					"cid" => $_GET ["cid"] 
			) )->find ();
			
			if (empty ( $info ['hash'] )) {
				$hash = $this->getHashCode ();
				$ob->where ( array (
						"cid" => $_GET ["cid"] 
				) )->save ( array (
						'hash' => $hash 
				) );
			}
			
			$this->assign ( "info", $info );
			$this->display ( 'Customer/detail' );
			return;
		}
		
		if ($_GET ["opt"] == 'introduction') { // 公司简介跳转
			$ob = M ( "csl_customer" );
			$info = $ob->where ( array (
					"cid" => $_GET ["cid"] 
			) )->find ();
			
			if (empty ( $info ['hash'] )) {
				$hash = $this->getHashCode ();
				$ob->where ( array (
						"cid" => $_GET ["cid"] 
				) )->save ( array (
						'hash' => $hash 
				) );
			}
			
			$this->assign ( "info", $info );
			$this->display ( 'Customer/introduction' );
			return;
		}
		
		if ($_GET ['opt'] == 'delete') { // delete
			$ob = M ( "csl_customer" );
			$ob->where ( array (
					'cid' => $_GET ['cid'] 
			) )->delete ();
			
			$this->redirect ( '/Admin/Customer/clist/' );
			return;
		}
		
		if ($_POST) { // 保存
			$ob = M ( "csl_customer" );
			if ($_POST ['cid'] != '') { // 修改
				$info = $ob->where ( array (
						"cid" => $_POST ["cid"] 
				) )->find ();
				
				// 手机号不能重复
				if ($info ['mobile'] != $_POST ['mobile']) {
					if (! checkmobile ( $_POST ['mobile'] )) {
						$this->error ( '该手机号已存在！', '/Admin/Customer/clist/cid/' . $info ['cid'] . '/opt/edit' );
					}
				}
				
				$this->uploadAvatar ();
				$ob->where ( array (
						'cid' => $_POST ['cid'] 
				) )->save ( $_POST );
				// dayin($ob->getlastsql());exit();
			} else { // 新增
			       // 手机号不能重复
				if (! checkmobile ( $_POST ['mobile'] )) {
					$this->error ( '该手机号已存在！', '/Admin/Customer/clist/opt/add' );
				}
				
				// $_POST['uid'] = session('info.uid'); //下单人
				$_POST ['ctime'] = date ( 'Y-m-d H:i:s', time () ); // 生成时间
				
				if (empty ( $_POST ['hash'] )) {
					$_POST ['hash'] = $this->getHashCode ();
				}
				
				$this->uploadAvatar ();
				
				$ob->add ( $_POST );
			}
			
			$this->redirect ( '/Admin/Customer/clist/cid/' . $_POST ['cid'] . '/opt/edit' );
			return;
		}
		
		// 查询
		
		if (session ( 'info.rid' ) == '1') {
			// 超级管理员
			$sql = array ();
		} elseif (session ( 'info.rid' ) == '2' || session ( 'info.rid' ) == '5' || session ( 'info.rid' ) == '7') {
			// 代理商 查看名下所有门店的所有客户
			$op = M ( 'csl_user' );
			$list = $op->where ( array (
					'puid' => session ( 'info.uid' ) 
			) )->select ();
			$uids = takearr ( $list, 'uid' );
			if ($uids != '()') {
				$sql = array (
						'_string' => " csl_customer.uid = '" . session ( 'info.uid' ) . "' or csl_customer.uid in $uids" 
				);
			} else {
				$sql = array (
						'_string' => " csl_customer.uid = '" . session ( 'info.uid' ) . "'" 
				);
			}
		} elseif (session ( 'info.rid' ) == '3') {
			// 门店
			$sql = array (
					'csl_customer.uid' => session ( 'info.uid' ) 
			);
		} elseif (session ( 'info.rid' ) == '4') {
			// 工厂
			$sql = array ();
		} else {
			
			$op = M ( 'csl_user' );
			$list = $op->where ( array (
					'puid' => session ( 'info.uid' ) 
			) )->select ();
			$uids = takearr ( $list, 'uid' );
			if ($uids != '()') {
				$sql = array (
						'_string' => " csl_customer.uid = '" . session ( 'info.uid' ) . "' or csl_customer.uid in $uids" 
				);
			} else {
				$sql = array (
						'_string' => " csl_customer.uid = '" . session ( 'info.uid' ) . "'" 
				);
			}
		}
		
		$key = $_GET ["key"];
		$name = $_GET ["name"];
		if ($name) {
			$sql [] = array (
					'csl_customer.' . $key => array (
							'like',
							"%{$name}%" 
					) 
			);
		}
		
		$ob = M ( "csl_customer" );
		
		$cpp = 20; // 分页数
		import ( 'ORG.Util.Page' ); // 导入分页类
		$count = $ob->where ( $sql )->count (); // 查询满足要求的总记录数
		$Page = new Page ( $count, $cpp ); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show (); // 分页显示输出
		
		$list = $ob->join ( "csl_user ON csl_customer.uid = csl_user.uid" )->field ( 'csl_customer.* , csl_user.name as csl_user_name , csl_user.username as csl_user_username' )->where ( $sql )->limit ( $Page->firstRow . ',' . $Page->listRows )->order ( array (
				'cid' => 'desc' 
		) )->select ();
		// $list = $ob->field('csl_customer.*,user.name as userName')->join(' csl_user user ON csl_customer.uid = user.uid')->order(array('cid'=>'desc'))->where($strsql)->limit($Page->firstRow.','.$Page->listRows)->select();
		
		foreach ( $list as $key => $value ) {
			if (is_file ( DATA_PATH . $value ['hash'] . '.png' )) {
				$list [$key] ['qrcode'] = '/Runtime/Data/' . $value ['hash'] . '.png';
			} else {
				$list [$key] ['qrcode'] = '/bizcard/introqrcode/hash/' . $value ['hash'];
			}
			
			//
		}
		
		// print_r($list);
		$this->assign ( "list", $list );
		$this->assign ( 'show', $show ); // 赋值分页输出
		
		$this->display ( "Customer/clist" );
	}
	function getHashCode() {
		$ob = M ( "csl_customer" );
		$hash = substr ( md5 ( time () . rand ( 10000, 999999 ) ), 0, 12 );
		$exist = $ob->where ( array (
				'hash' => $hash 
		) )->find ();
		$i = 0;
		while ( $exist ) {
			$i ++;
			if ($i > 5) {
				echo "程序错误请检查";
				exit ();
				break;
			}
			$hash = substr ( md5 ( time () . rand ( 10000, 999999 ) ), 0, 12 );
			$exist = $ob->where ( array (
					'hash' => $hash 
			) )->find ();
		}
		
		return $hash;
	}
	function uploadAvatar() {
		return true;//取消上传图片
		
		import ( 'ORG.Net.UploadFile' );
		$upload = new UploadFile (); // 实例化上传类
		$upload->maxSize = 3145728; // 设置附件上传大小
		$upload->thumb = true; // /设置需要生成缩略图，仅对图像文件有效
		$upload->thumbMaxWidth = 150; // 设置附件上传大小
		$upload->thumbMaxHeight = 150; // 设置附件上传大小
		$upload->thumbPrefix = ''; // 设置附件上传大小
		$upload->allowExts = array (
				'jpg',
				'gif',
				'png',
				'jpeg' 
		); // 设置附件上传类型
		$upload->savePath = TEMP_PATH; // 设置附件上传目录
		if (! $upload->upload ()) { // 上传错误提示错误信息
			return $upload->getErrorMsg ();
		} else { // 上传成功 获取上传文件信息
			$info1 = $upload->getUploadFileInfo ();
			
			$image_file = $info1 [0] ['savepath'] . $info1 [0] ['savename'];
			$image_info = getimagesize ( $image_file );
			$base64_image_content = "data:{$image_info['mime']};base64," . (base64_encode ( file_get_contents ( $image_file ) ));
			@unlink ( $image_file );
			
			if (isset ( $_FILES ['Avatar'] ))
				$_POST ['Avatar'] = $base64_image_content;
			
			if (isset ( $_FILES ['pay_two_code'] ))
				$_POST ['pay_two_code'] = $base64_image_content;
			
			return true;
		}
	}
	
	// 支付二维码
	function uploadTwoCode() {
		if ($_GET ['opt'] == 'up') { // 跳转
			$info ['cid'] = $_GET ['cid'];
			$this->assign ( 'info', $info );
			$this->display ( 'Customer/upload' );
			return;
		}
		
		if ($_POST) {
			$ob = M ( "csl_customer" );
			$this->uploadAvatar ();
			$ob->where ( array (
					'cid' => $_POST ['cid'] 
			) )->save ( $_POST );
			
			$this->redirect ( '/Admin/Customer/clist/' );
			return;
		}
	}
	function amap() {
		$url = "http://restapi.amap.com/v3/place/text?";
		$url .= "key=846cde7d790d0a51e901b1a36e83b53e";
		$url .= "&keywords=" . $_POST ['keywords'] . "&types=&city=" . $_POST ['city'] . "";
		$url .= "&children=1&offset=20&page=1&extensions=all";
		
		$re = file_get_contents ( $url );
		
		$json = json_decode ( $re, true );
		
		if ($json ['info'] == 'OK') {
			$zL = $json ['pois'] [0] ['location'];
			$zLarray = explode ( ',', $zL );
			
			$result ['location'] = array (
					'Lng' => $zLarray [0],
					'Lat' => $zLarray [1] 
			);
			$result ['info'] = "OK";
		} else {
			$result ['info'] = "ERROR";
		}
		
		echo json_encode ( $result );
		;
	}
	
	
	///Admin//Customer/crelation/uid/53
	function crelation(){
		
		if (session ( 'info.rid' ) == '1') {
			$uid = $_POST['uid']?$_POST['uid']:session('info.uid');
			
			$oc = M('csl_user');
			$ulist = $oc->order(['path'=>'asc'])->select();
			
		}elseif (session ( 'info.rid' ) == '5') {
			//官方
			$uid = session('info.uid');
			$ou = new UserModel ();
			$ulists = $ou->getAllChildrenUser($uid);
			$ulist = $ou->getAllChildrenUser($uid,2);
			if(in_array($_POST['uid'], $ulists)){
				$uid = $_POST['uid'];
			}
		}else{
			//核心
			$uid = session('info.uid');
			$ulist = [];
		}
		
		if($_GET['do']=='init' && $_POST['level']=='A'){
			//第一级
			$ob = M("csl_customer");
			$list = $ob->where(['uid'=>$uid,'parentcid'=>'0'])
						->field(['mobile','name','money','point','cid','parentcid'])
						->order(['money'=>'desc','point'=>'desc','cid'=>'desc'])
							->select();
			
			echo json_encode($list);
			return ;
		}elseif($_GET['do']=='more' && $_POST['level']=='B'){
			//第二级
			$uid = $_POST['uid'];
			if(empty($uid)){
				$uid = session('info.uid');
			}
			$ob = M("csl_customer");
			$list = $ob->where(['uid'=>$uid,'parentcid'=>$_POST['cid']])
						->field(['mobile','name','money','point','cid','parentcid'])
						->order(['money'=>'desc','point'=>'desc','cid'=>'desc'])
							->select();
			
			$pcid = [];
			foreach($list as $key=>$value){
				$pcid[] = $value['cid'];
			}
			$clist = $ob->where(['uid'=>$uid,'parentcid'=>['in',$pcid]])
								->field(['mobile','name','money','point','cid','parentcid'])
								->order(['money'=>'desc','point'=>'desc','cid'=>'desc'])
									->select();
			foreach($list as $key=>$value){
				foreach($clist as $key1=>$value1){
					if($value['cid']==$value1['parentcid']){
						$list[$key]['Cchild'][] = $value1;
						unset($clist[$key1]);
					}
				}
			}
			echo json_encode($list);
			return ;
		}
		
		
		$this->assign('ulist',$ulist);
		$this->display();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

?>